<template>
    <div>
        <el-form :model="User" status-icon :rules="rules" ref="User" label-width="140px" class="demo-User">
            <el-form-item label="userName" prop="userName">
                <el-input v-model="User.userName"></el-input>
            </el-form-item>
            <el-form-item label="name" prop="name">
                <el-input v-model="User.name"></el-input>
            </el-form-item>
            <el-form-item label="age" prop="age">
                <el-input v-model="User.age"></el-input>
            </el-form-item>
            <el-form-item label="email" prop="email">
                <el-input v-model="User.email"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('User')">搜索</el-button>
                <el-button @click="resetForm('User')">清空</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="users" border style="width: 100%" height=600>
            <el-table-column
                    fixed
                    prop="id"
                    label="id"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="userName"
                    label="username"
                    width="150">
            </el-table-column>
            <el-table-column
                    prop="password"
                    label="password"
                    width="200">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="name"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="age"
                    label="age"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="email"
                    label="email"
                    width="200">
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="prev, pager, next"
                page-size="4"
                :total="totals"
                @current-change="page">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "Search",
        methods: {
            submitForm(formName) {
                const _this = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.get('http://localhost:8081/user/search?page=1&size=5&username='
                            + _this.User.userName+'&name='+_this.User.name+'&age='+_this.User.age+'&email='
                            + _this.User.email).then(function (response) {
                            _this.users=response.data.records;
                            _this.totals=response.data.total;
                            if(_this.totals<=0){
                                alert("暂无数据");
                            }
                        })
                    } else {
                        console.log('不可搜索');
                        return false;
                    }
                });
            },
            page(currentPage){
                const _this = this;
                axios.get('http://localhost:8081/user/search?page='+currentPage+'&size=5&username='
                    + _this.User.userName+'&name='+_this.User.name+'&age='+_this.User.age+'&email='
                    + _this.User.email).then(function (response) {
                    _this.users=response.data.records;
                    _this.totals=response.data.total;
                    if(_this.totals<=0){
                        alert("暂无数据");
                    }
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        data(){
            let checkAge = (rule, value, callback) => {

                if (value&&!(/^[+]?(\d+)$|^[+]?(\d+\.\d+)$/).test(value)) {
                    callback(new Error('请输入数字'));
                } else {
                    callback()
                }
            };
            return{
                User: {
                    userName: '',
                    name: '',
                    age: '',
                    email: '',
                },
                rules: {
                    userName: [
                        { min: 2, max: 32, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    name: [],
                    age: [
                        { validator:checkAge, trigger: 'blur' }
                    ],
                    email: [],
                },
                totals:0,
                users:[]
            }
        }
    }
</script>